﻿$(document).ready(function () {
    var J_loginstatus = $("#J_loginstatus");
    var light = $("#J_light");
    var J_item = $("#J_item");
    J_item.find("button").click(function () {
        //no login
        if (J_loginstatus.length > 0) {
            location.href = J_loginstatus.attr("href");
            return;
        };
        //exec
        var t = $(this);
        var url = J_item.attr("data-action");
        var data = {};
        data[t.attr("name")] = t.val();
        light.show();
        light.find(".light-bg").css("height", $(document).height());
        light.find(".light-box").css({ left: (J_item.offset().left + 230), top: t.offset().top - 20 });
        light.find(".light-iframe").css("height", $(document).height());
        var html = "提交中...";
        light.find(".light-bd").html(html);
        $.ajax({
            async: true,
            type: "post",
            url: url,
            data: data,
            dataType: "text",
            success: function (msg) {
                var data = eval("(" + msg + ")");
                var unusedTimes = data["unusedTimes"];
                var res = data["awardRes"];
                t.prev("p").find("span").html(unusedTimes);
                light.find(".light-bd").html(res);
                return false;
            },
            error: function () {
                light.find(".light-bd").html("抱歉，出错了！请刷新页面后再试。");
                return false;
            }
        });
        return false;
    });
    light.find(".light-close").click(function () {
        light.hide();
    });
    //提示关闭
    $("#J_light_sign .light-close").click(function () {
        $("#J_light_sign").hide();
    });

    //login
    var J_form = $("#J_form");
    J_form.submit(function () {
        var t = $(this);
        var input = t.find("input");
        if (input.eq(0).val().length < 1) { alert("请填写用户名"); return false; };
        if (input.eq(1).val().length < 1) { alert("请填写密码"); return false; };
        return true;
    });


});

var clock = $("#J_clock");
var num = parseInt(clock.attr("data-time"));
function count() {
    num = num - 1;
    d = Math.floor(num / 86400);
    h = Math.floor((num - d*86400) / 3600);
    m = Math.floor((num - d * 86400 - h * 3600) / 60);
    dd = d > 0 ? "<span>" + d + "</span>" + "天" : "";
    hh = h > 0 ? "<span>" + h + "</span>" + "小时" : "";
    mm = m > 0 ? "<span>" + m + "</span>" + "分钟" : "";
    clock.html("活动倒计时:" + dd + hh + mm);
    if (num < 1) {
        clearInterval(countdown);
        clock.html("活动已结束");
    };
};
countdown = setInterval(count, 1000);